<?php

function send_text($content, $mentioned_mobile_list, $url) {
  $post_data = array(
    "msgtype" => "text",
    "text" => array(
      "content" => $content,
      "mentioned_mobile_list" => $mentioned_mobile_list,
    ),
  );
  $res = request_post(json_encode($post_data, '320'), 'json', $url);
  // print_r($res);
  return $res;
}

function send_markdown($content, $url) {
  $post_data = array(
    "msgtype" => "markdown",
    "markdown" => array(
      "content" => $content,
    ),
  );
  $res = request_post(json_encode($post_data, '320'), 'json', $url);
  // print_r($res);
  return $res;
}

function request_post($post_data = array(), $dataType = '', $url) {
  if (empty($url) || empty($post_data)) {
    return false;
  }
  // save_log('POST', json_encode($post_data, JSON_UNESCAPED_UNICODE));
  $curlPost = $post_data;
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  if ($dataType == 'json') {
    curl_setopt(
      $ch,
      CURLOPT_HTTPHEADER,
      array(
        'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
        'Content-Length: ' . strlen($curlPost),
      )
    );
  }
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  $data = curl_exec($ch);
  // save_log('RESPONSE', $data);
  return $data;
}